package com.taxi.service;

import com.taxi.constant.DriverCarConstants;
import com.taxi.constant.IdentityConstant;
import com.taxi.constant.TokenConstants;
import com.taxi.remote.ServiceDriverUserClient;
import com.taxi.remote.ServiceVefificationcodeClient;
import com.taxi.response.DriverUserExistsResponse;
import com.taxi.response.NumberCodeResponse;
import com.taxi.response.TokenResponse;
import com.taxi.result.Result;
import com.taxi.result.ResultCodeEnum;
import com.taxi.util.JwtUtils;
import com.taxi.util.RedisPrefixUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @Author: aniu
 * @Date: 2023/9/28
 * @ClassName: VerificationCodeService
 * @Description: Write class functionality and purpose here.
 * @Version: 1.0
 */
@Service
public class VerificationCodeService {
    @Autowired
    private ServiceDriverUserClient serviceDriverUserClient;
    @Autowired
    ServiceVefificationcodeClient serviceVefificationcodeClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    public Result checkAndsendVerificationCode(String driverPhone) {
        //查询service-driver-user,该手机号的司机是否存在
        DriverUserExistsResponse data = serviceDriverUserClient.getUser(driverPhone).getData();
        int ifExists = data.getIfExists();
        if(ifExists!= DriverCarConstants.DRIVER_EXISTS){
            return Result.fail("司机不存在");
        }
        //获取验证码
        NumberCodeResponse num = serviceVefificationcodeClient.getNumberCode(6).getData();
        //调用第三方发送验证码
        System.out.println("验证码:" + num.getNumberCode());

        //存入redis
        String key = RedisPrefixUtils.generatorDriverKeyByPhone(driverPhone, IdentityConstant.DRIVER_IDENTITY);
        redisTemplate.opsForValue().set(key,num.getNumberCode()+"",2, TimeUnit.MINUTES);
        return Result.ok(num.getNumberCode());
    }

    public Result checkCode(String driverPhone,String verificationCode){
        if(ObjectUtils.isEmpty(driverPhone)||ObjectUtils.isEmpty(verificationCode)){
            ResultCodeEnum anEnum = ResultCodeEnum.VERIFICATION_CODE_ERROR;
            return Result.build(anEnum.getCode(), anEnum.getMessage());
        }

        //根据手机号,去redis读取验证码
        String key=RedisPrefixUtils.generatorDriverKeyByPhone(driverPhone,IdentityConstant.DRIVER_IDENTITY);
        String codeRedis = redisTemplate.opsForValue().get(key);

        //校验验证码
        if (!verificationCode.trim().equals(codeRedis.trim())||StringUtils.isEmpty(codeRedis)) {
            ResultCodeEnum anEnum = ResultCodeEnum.CHECK_CODE_ERROR;
            return Result.build(anEnum.getCode(),anEnum.getMessage());
        }

        //颁发令牌,不应该用魔法值,用常量
        String accessToken = JwtUtils.generatorToken(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshToken=JwtUtils.generatorToken(driverPhone,IdentityConstant.DRIVER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        //将token存到redis当中
        String accessTokenKey = RedisPrefixUtils.generatorTokenKey(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        redisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);//过期时间30天

        String refreshTokenKey = RedisPrefixUtils.generatorTokenKey(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);
        redisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);//过期时间31天

        //响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return Result.ok(tokenResponse);
    }
}
